#include<iostream>
#include<string>

using namespace std;

int charToNum(char c)
{
    if(c>='0'&&c<='9')return c-'0';
    else if(c>='a'&&c<='z')  return c-'a'+10;
    else return -1;
}

long strToNum(string &str,long long radix)
{
    long long ret;
    for(int i = 0; i < str.length(); i++) {
        if(str[i]>='0'&&str[i]<='9') {
            ret = ret*radix+(str[i]-'0');
        }
        else if(str[i]>='a'&&str[i]<='z') {
            ret = ret*radix+(str[i]-'a'+10);
        }
    }
    return ret;
}
    
int main(void)
{
    string N1,N2;
    cin>>N1>>N2;
    int tag;
    cin>>tag;
    long long radix;
    cin>>radix;
    long long result = 0;
    if(tag == 2) {
        string temp = N1;
        N1 = N2;
        N2 = temp;
    }
    result = strToNum(N1,radix);
    printf("N1=%d\n",result);
    char maxC = '0';
    for(int i = 0; i < N2.length(); i++) {
        if(N2[i] > maxC) maxC = N2[i];
    } 
    int r2;
    long long result2 = 0;
    for(r2 = charToNum(maxC)+1; result2 < result;r2++) {
        result2 = strToNum(N2,r2);
        printf("r2=%d,result2=%d",r2,result2);
    }
    printf("r2=%d\n",r2);
    if(result2 == result)
        printf("%d\n",r2);
    else
        printf("Impossible\n");
}